import Vue from 'vue'
import $i from './index.js'
import validation from '@/depend/js/test.js'
// import store from '@/store'
export function jumpUrl(data, type = 0) {
    let isToken =uni.getStorageSync('_user_token') || '11111'
    if (!isToken) {
        uni.showModal({
            title: '提示',
            content: '您还未登录',
            showCancel:false,
            success: function(res) {
                if (res.confirm) {
                    uni.redirectTo({
                        url: '/pages/reg/login.vue'
                    })
                }
            }
        })
        return
    }

    if (data) {
        if (type == 0) {
            uni.navigateTo({
                url: data
            })
        } else if (type == 1) {
            uni.redirectTo({
                url: data
            })
        } else if (type == 2) {
            uni.switchTab({
                url: data
            })
        }
    } else {
        $g.toast('跳转链接有误，请联系管理员')
    }
}

// 添加单位
export function addUnit (value = 'auto', unit = 'rpx') {
	value = String(value)
	return validation.number(value) ? `${value}${unit}` : value
}
// 生成uuid
export function createUuid (len, first = '', radix = null) {
	let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
	let uuid = []
	radix = radix || chars.length
	
	if (len) {
		for (let i = 0; i < len; i++) {
			uuid[i] = chars[0 | Math.random() * radix]
		}
	} else{
		let r
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
		uuid[14] = '4'
		
		for (let i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
			}
		}
	}
	
	if (first) {
		for (let i = 0; i < first.length; i++) {
			uuid[i] = first[i]
		}
	}
	return uuid.join('')
}
/* 轻提示 */
function toast(data) {
    let parameter = {
        title: '',
        icon: 'none',
        mask: false,
        duration: 2000,
        position: 'bottom',
        onClose: () => {}
    }
    if (typeof data === 'string') {
        parameter.title = data
    } else {
        parameter = Object.assign(parameter, data)
    }
    uni.showToast(parameter)
    setTimeout(() => {
        data.onClose && data.onClose()
    }, parameter.duration)
}

/* 跳转链接 */
function openUrl(url, options = {}) {
    if (!url) return
    if (url === 'wait') {
        toast('敬请期待')
    }
    if (url.startsWith('http')) {
        /* 完整外链地址处理 */
        // #ifdef MP-WEIXIN
        // const isWhite = $i.wxWhiteList.findIndex(el => {
        //     return url.indexOf(el) !== -1
        // })
        if (isWhite === -1) {
            uni.setClipboardData({
                data: url
            })
            toast('该页面无法直接在小程序内打开。已自动复制网址，请在手机浏览器里粘贴后访问')
        } else {
            let encodeUrl = encodeURIComponent(url)
            uni.navigateTo({
                url: '/pages/webview/webview?url=' + encodeUrl
            })
        }
        // #endif
        // #ifdef APP-PLUS
        let enUrl = encodeURIComponent(url)
        uni.navigateTo({
            url: '/pages/webview/webview?url=' + enUrl
        })
        // #endif
        // #ifdef H5
        window.location.href = url
        // #endif
    } else if (!url.startsWith('/pages')) {
        toast('连接错误，无法跳转')
        console.log('连接错误，无法跳转')
    } else {
        const isSwitchTab = $i.tabRouter.some(el => (el === url))
        if (isSwitchTab) {
            uni.switchTab({
                url
            })
        } else if (options.redirect === 1) {
            uni.redirectTo({
                url
            })
        } else {
            uni.navigateTo({
                url
            })
        }
    }
}
 
function loginOpenUrl(url, redirect = 0) {
    if (store.state.vx_isLogin) {
        openUrl(url)
    } else {
        const pages = getCurrentPages()
        const page = pages[pages.length - 1]
        if (page === 'pages/reg/login') return
        store.commit('setStateValue', {
            name: 'vx_loginUrl',
            value: {
                url: page.route,
                query: page.options || page.$route.query
            }
        })
        let login
        login = '/pages/reg/login'
        if (redirect === 0) {
            console.log("navigateTo: ", login)
            uni.navigateTo({
                url: login
            })
        } else {
            console.log("redirect: ", login)
            uni.redirectTo({
                url: login
            })
        }
    }
}

function toggelStatus(name, status) {
    if (status === 0) {
        this[name] = false
    } else if (status === 1) {
        this[name] = true
    } else {
        this[name] = !this[name]
    }
}

function finishForm(form, filters = []) {
    return Object.keys(form).every(key => {
        let filter = filters.some(el => (el === key))
        return filter || form[key]
    })
}

 var ISCANVAS2D = true;
 
 switch (uni.getSystemInfoSync().platform) {
 	case 'android':
 		ISCANVAS2D = true
 		break;
 	case 'ios':
 		ISCANVAS2D = true
 		break;
 	default:
 		ISCANVAS2D = false
 		break;
 }
 
Vue.prototype.$f = {
    toast,
    openUrl,
    loginOpenUrl,
    toggelStatus,
    finishForm,
    jumpUrl,
    ISCANVAS2D
}

export default {
    toast,
    openUrl,
    loginOpenUrl,
    jumpUrl,
    ISCANVAS2D
}
